// 切换标签页
document.querySelectorAll('.tab-btn').forEach(button => {
    button.addEventListener('click', () => {
        // 移除所有活动状态
        document.querySelectorAll('.tab-btn').forEach(btn => {
            btn.classList.remove('active');
        });
        document.querySelectorAll('.tab-content').forEach(content => {
            content.style.display = 'none';
        });

        // 激活当前标签
        button.classList.add('active');
        const tabId = button.getAttribute('data-tab');
        document.getElementById(`${tabId}-content`).style.display = 'block';
    });
});

// 登录表单提交
document.getElementById('login-form').addEventListener('submit', async (e) => {
    e.preventDefault();

    const user_id = document.getElementById('login-userid').value;
    const password = document.getElementById('login-password').value;
    const messageElement = document.getElementById('login-message');

    try {
        const response = await fetch('/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: new URLSearchParams({
                'user_id': user_id,
                'password': password
            })
        });

        const data = await response.json();

        if (data.success) {
            messageElement.textContent = '登录成功，正在跳转...';
            messageElement.className = 'form-message success';
            // 跳转到相应页面
            setTimeout(() => {
                window.location.href = data.redirect;
            }, 1000);
        } else {
            messageElement.textContent = data.message;
            messageElement.className = 'form-message error';
        }
    } catch (error) {
        messageElement.textContent = '登录失败，请重试';
        messageElement.className = 'form-message error';
        console.error('登录错误:', error);
    }
});

// 注册表单提交
document.getElementById('register-form').addEventListener('submit', async (e) => {
    e.preventDefault();

    const user_id = document.getElementById('register-userid').value;
    const password = document.getElementById('register-password').value;
    const messageElement = document.getElementById('register-message');

    if (user_id.length < 3) {
        messageElement.textContent = '用户ID长度不能少于3个字符';
        messageElement.className = 'form-message error';
        return;
    }

    if (password.length < 3) {
        messageElement.textContent = '密码长度不能少于3个字符';
        messageElement.className = 'form-message error';
        return;
    }

    try {
        const response = await fetch('/register', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/x-www-form-urlencoded',
            },
            body: new URLSearchParams({
                'user_id': user_id,
                'password': password
            })
        });

        const data = await response.json();

        if (data.success) {
            messageElement.textContent = '注册成功，请登录';
            messageElement.className = 'form-message success';

            // 切换到登录标签
            setTimeout(() => {
                document.querySelector('.tab-btn[data-tab="login"]').click();
                document.getElementById('login-userid').value = user_id;
            }, 1500);
        } else {
            messageElement.textContent = data.message;
            messageElement.className = 'form-message error';
        }
    } catch (error) {
        messageElement.textContent = '注册失败，请重试';
        messageElement.className = 'form-message error';
        console.error('注册错误:', error);
    }
});

document.getElementById('login-form').addEventListener('submit', async function(e) {
    e.preventDefault(); // 阻止默认表单提交

    const formData = new FormData(this);
    const data = Object.fromEntries(formData.entries());

    try {
        const response = await fetch('/login', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
        });

        const result = await response.json();
        if (result.success) {
            window.location.href = result.redirect; // 实现跳转
        } else {
            document.getElementById('login-message').textContent = result.message;
        }
    } catch (error) {
        console.error('登录请求失败:', error);
    }
});